//
//  CDColumnController.m
//  栏目中心Demo
//
//  Created by liaoxingchi on 16/9/23.
//  Copyright © 2016年 liaoxingchi. All rights reserved.
//

#import "CDColumnController.h"
#import "Masonry.h"
#import "CDColumnSubscriptionCell.h"
#import "CDColumnDetailController.h"
static NSString *cellID = @"cellid";
@interface CDColumnController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UITableView *columnTableView;
@property (nonatomic, strong) NSArray *columnArray;
@property (nonatomic,weak) CDPullDownLoadView *pullDownView;
@end

@implementation CDColumnController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLoadView];
    self.view.backgroundColor = [UIColor darkGrayColor];
    [self setupUI];
    [self loadColumnData];
//    NSLog(@"%@",self.columnArray);
}

// 初始化UI
- (void)setupUI {
    
    // 创建控件
    // 栏目
    UITableView *columnTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    UIView *signView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 55)];
    UIButton *signBtn = [[UIButton alloc] init];
    [signBtn setTitle:@"登录" forState:UIControlStateNormal];
    signBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [signBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    [signBtn sizeToFit];
    [signView addSubview:signBtn];
    
    signView.backgroundColor = [UIColor lightGrayColor];
    UILabel *signLabel = [[UILabel alloc] init];
    signLabel.text = @"后可查看那些栏目你已关注";
    signLabel.textColor = [UIColor darkGrayColor];
    signLabel.font = [UIFont systemFontOfSize:12];
    [signView addSubview:signLabel];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor yellowColor];
    [signView addSubview:lineView];
    columnTableView.tableHeaderView = signView;
    
    
    columnTableView.backgroundColor = [UIColor clearColor];
    // 设置行高
    columnTableView.rowHeight = 115;
    columnTableView.rowHeight = UITableViewAutomaticDimension;
    columnTableView.estimatedRowHeight = 128;
    // 遵守代理
    columnTableView.delegate = self;
    columnTableView.dataSource = self;
    // 设置线
    columnTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 滚动条
    columnTableView.showsVerticalScrollIndicator =
    NO;
    columnTableView.showsHorizontalScrollIndicator = NO;
    // 注册单元格
    [columnTableView registerClass:[CDColumnSubscriptionCell class] forCellReuseIdentifier:cellID];
    [self.view addSubview:columnTableView];
    
    // 自动布局
    [columnTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view);
//        make.left.bottom.right.equalTo(self.view);
        make.edges.equalTo(self.view);
    }];
//    [signView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.equalTo(self.view);
//        make.height.offset(55);
//    }];
    [signBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(signView);
        make.left.equalTo(self.view).mas_offset(103);
    }];
    [signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(signBtn.mas_right).mas_offset(-3);
        make.centerY.equalTo(signBtn);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(signBtn);
        make.bottom.equalTo(signBtn.mas_bottom).mas_offset(-5);
        make.height.offset(1);
        make.width.offset(28);
    }];
    
    
    self.columnTableView = columnTableView;
}



#pragma mark - UITableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CDColumnSubscriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
//    cell.textLabel.text = @"么么哒";
    
    return cell;
}

//
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CDColumnDetailController *detailVC = [[CDColumnDetailController alloc] init];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    [self.columnTableView deselectRowAtIndexPath:indexPath animated:NO];
}


#pragma mark - 加载数据
- (void)loadColumnData {
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"columnurl.plist" ofType:nil];
//
//    NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
    self.columnArray = [NSArray objectListWithPlistName:@"columnurl.plist" clsName:@"CDAllColumnModel"];
}

-(void)addLoadView{
    CDPullDownLoadView *pullDownView = [[CDPullDownLoadView alloc] init];
    self.pullDownView = pullDownView;
    [self.view addSubview:pullDownView];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.pullDownView scrollViewScroll:scrollView];
}
@end
